package org.dragonnova.meetingapp.app.fragment.guide;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.utils.Constants;
import org.dragonnova.meetingapp.utils.LogUtil;

public class MeetingCreateFragment extends FragmentImpl implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private int mParam1;
    private String mParam2;

    private View mContentView;
    private TextView mguide_tv_id;
    private TextView mguide_tv_name;
    private TextView mguide_tv_password;

    public MeetingCreateFragment() {
        // Required empty public constructor
    }

    public static MeetingCreateFragment newInstance(int param1, String param2) {
        MeetingCreateFragment fragment = new MeetingCreateFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.eee("fragment created==============");
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_meeting_create, container, false);
        initView();
        return mContentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initView() {
        mguide_tv_name = (TextView) mContentView.findViewById(R.id.mguide_tv_name);
        mguide_tv_id = (TextView) mContentView.findViewById(R.id.mguide_tv_id);
        mguide_tv_password = (TextView) mContentView.findViewById(R.id.mguide_tv_password);
        Button mguide_bt_create = (Button) mContentView.findViewById(R.id.mguide_bt_create);
        Button mguide_bt_close = (Button) mContentView.findViewById(R.id.mguide_bt_cancel);
        LinearLayout mguide_ll_id = (LinearLayout) mContentView.findViewById(R.id.mguide_ll_id);

        switch (mParam1) {
            case Constants.ACTIVITY_ACTION_CREATOR:
                mguide_tv_id.setText(mParam2);
                break;
            case Constants.ACTIVITY_ACTION_RESETOR:
                mguide_ll_id.setVisibility(View.GONE);
                mguide_bt_close.setVisibility(View.GONE);
        }

        mguide_bt_create.setOnClickListener(this);
        mguide_bt_close.setOnClickListener(this);
    }

    private void initData() {


    }

    // TODO: Rename method, updateMeeting argument and hook method into UI event
    public void interact(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.eee("activity destoryed==============");
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.mguide_bt_create:
                attemptCreate();
                break;
            case R.id.mguide_bt_cancel:
                attemptCancel();
                break;
        }
    }

    private void attemptCreate() {

        // Reset errors.
        mguide_tv_name.setError(null);
        mguide_tv_password.setError(null);

        // Store values at the time of the login attempt.
        String name = mguide_tv_name.getText().toString();
        String password = mguide_tv_password.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            mguide_tv_password.setError(getString(R.string.error_invalid_password));
            focusView = mguide_tv_password;
            cancel = true;
        }

        // Check for a valid email address.
        if (TextUtils.isEmpty(name)) {
            mguide_tv_name.setError(getString(R.string.error_field_required));
            focusView = mguide_tv_name;
            cancel = true;
        } else if (!isEmailValid(name)) {
            mguide_tv_name.setError(getString(R.string.error_invalid_email));
            focusView = mguide_tv_name;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        } else {
            // Show a progress spinner, and kick off a background task to
            // perform the user login attempt.
            switch (mParam1) {
                case Constants.ACTIVITY_ACTION_CREATOR:
                    Uri uri1 = Uri.parse("content://MeetingCreateFragment" + "/" + "create" + "/" + mguide_tv_id.getText().toString() + "/" + mguide_tv_name.getText().toString() + "/" + mguide_tv_password.getText().toString());
                    interact(uri1);
                    break;
                case Constants.ACTIVITY_ACTION_RESETOR:
                    Uri uri2 = Uri.parse("content://MeetingCreateFragment" + "/" + "create" + "/" + "-1" + "/" + mguide_tv_name.getText().toString() + "/" + mguide_tv_password.getText().toString());
                    interact(uri2);
            }

        }
    }

    private void attemptCancel() {
        Uri uri = Uri.parse("content://MeetingCreateFragment" + "/" + "cancel");
        interact(uri);
    }

    private boolean isEmailValid(String email) {
        //TODO: Replace this with your own logic
        //return email.contains("@");
        return email.length() > 0;
    }

    private boolean isPasswordValid(String password) {
        //TODO: Replace this with your own logic
        //return password.length() > 4;
        return true;
    }
}
